<template>
  <div class="addTags-container">
    <el-dialog
      :title="isEdit ?'修改标签':'新增标签'"
      :visible.sync="dialogFormVisible"
      width="35%"
      @close="closeDialog"
      @open="openEvent"
    >
      <el-form :model="tagsForm" ref="tagsFormRef" :rules="tagsFormRules">
        <el-form-item label="所属学科" prop="subjectID" label-width="80px" v-if="!subjectsId">
          <el-select v-model="tagsForm.subjectID" placeholder="请选择">
            <el-option
              :label="item.label"
              :value="item.value"
              v-for="(item,index) in simpleSubjectsList"
              :key="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标签名称" prop="tagName" label-width="80px">
          <el-input v-model="tagsForm.tagName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible =false">取 消</el-button>
        <el-button type="primary" @click="onConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 获得学科的简单列表
import { simpleSubjects } from '@/api/hmmm/subjects'
import { addTags, tagsMessage, update } from '@/api/hmmm/tags'
export default {
  name: 'TagsAdd',
  data() {
    return {
      dialogFormVisible: false, // 控制弹出层的显示与隐藏
      tagsForm: {}, // 表单数据
      tagsFormRules: {
        // 表单验证
        subjectID: [{ required: true, message: '请选择学科名称', trigger: 'change' }],
        tagName: [{ required: true, message: '请输入标签名称', trigger: 'blur' }],
      },
      simpleSubjectsList: [], // 科学简单列表
    }
  },
  props: {
    isEdit: {
      type: Boolean,
      required: true,
    },
    userId: {
      type: [Number, String],
      required: true,
    },
    subjectsId: {
      type: [Number, String],
      required: true,
    },
  },
  watch: {
    subjectsId() {},
  },
  created() {
    this.getSimpleSubjects()
  },
  methods: {
    // 显示弹出框
    showDialog() {
      this.dialogFormVisible = true
    },
    // 获取学科的简单列表
    async getSimpleSubjects() {
      try {
        const { data: res } = await simpleSubjects()
        this.simpleSubjectsList = res
        console.log(res)
      } catch (error) {
        this.$message.error('获取简单科学列表数据失败')
      }
    },
    // 关闭弹出框的事件 重置表单
    closeDialog() {
      // 重置表单
      this.$refs.tagsFormRef.resetFields()
    },
    // 添加和修改标签
    onConfirm() {
      console.log(this.tagsForm)
      // 表单预校验
      this.$refs.tagsFormRef.validate(async (valid) => {
        if (!valid) {
          return
        }
        if (this.subjectsId) {
          this.tagsForm.subjectID = this.subjectsId
        }
        try {
          if (this.isEdit) {
            this.tagsForm.id = this.userId
            await update(this.tagsForm)
            this.$message.success('修改成功')
          } else {
            // 发送请求添加目录
            await addTags(this.tagsForm)
            //添加成功提示
            this.$message.success('添加成功')
          }
          // 关闭弹出框
          this.dialogFormVisible = false
          // 渲染列表 调用父组件上面的方法
          this.$parent.loadTagsList()
        } catch (error) {
          console.log(error)
          this.$message.error('添加失败')
        }
      })
    },
    // 打开对话框 获取标签详情
    async openEvent() {
      if (this.isEdit) {
        try {
          const { data: res } = await tagsMessage(this.userId)
          this.tagsForm = res
          console.log(res)
        } catch (error) {
          console.log(error)
          this.$message.error('获取标签详情失败')
        }
      }
    },
  },
}
</script>

<style scoped lang='less'>
.el-select {
  width: 100%;
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
</style>
